package cn.tedu.boot05.controller;

import cn.tedu.boot05.controller.entity.User;
import cn.tedu.boot05.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    UserMapper mapper;
    @RequestMapping("/reg")
    public String reg(User user){
        System.out.println("user = " + user);
        //拿用户输入的用户名从数据库里面查询
        User u = mapper.selectByUsername(user.getUsername());
        if (u!=null){
            return "用户名已存在!";
        }
        //把用户信息保存到数据库....
        mapper.insert(user);
        return "注册成功!<a href='/'>返回首页</a>";
    }

    public String login(User user){
        System.out.println("user = " + user);
        //通过用户输入的用户名查询到数据库里面的密码
        User u = mapper.selectByUsername(user.getUsername());
        if (u!=null){//代表查询到了
            //拿用户输入的密码和查询到的密码比较
            if (u.getPassword().equals(user.getPassword())){
                return "登陆成功!<a href='/'>回到首页</a>";
            }
            return "密码错误!<a href='/login.html'>重新登录</a>";
        }
        return "用户名不存在!<a href='/login.html'>重新登录</a>";
    }
}
